package com.huaxinkai.mobile.util.common;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONObject;
/**
 * http方法
 * @author hua
 *
 */
public class HttpUtil {

	/**
	 * doGet方法
	 * @param url 地址
	 * @return
	 */
	public static JSONObject doGetJSON(String url){
//		DefaultHttpClient client1 = new DefaultHttpClient();
		
		 CloseableHttpClient client = HttpClients.createDefault();
		 //= HttpClientUtil.createDefault();
		 
		HttpGet httpGet = new HttpGet(url);
		JSONObject jsonObject = null;
		try {
			HttpResponse httpResponse = client.execute(httpGet);
			HttpEntity entity = (HttpEntity) httpResponse.getEntity();
			if(entity != null){
				String result = EntityUtils.toString(entity,"UTF-8");
				jsonObject = JSONObject.fromObject(result);
			}
		} catch (Exception e) {
		}
		return jsonObject;
	}
	/**
	 * post获取数据
	 * @param url 地址
	 * @param outStr 发送的文本
	 * @return
	 */
	public static JSONObject doPostJSON(String url,String outStr){
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		JSONObject jsonObject = null;
		httpPost.setEntity(new StringEntity(outStr,"UTF-8"));
		try {
			HttpResponse response = httpClient.execute(httpPost);
			String result = EntityUtils.toString(response.getEntity(),"UTF-8");
			jsonObject = JSONObject.fromObject(result);
		} catch (Exception e) {
		}
		return jsonObject;
	}
}
